/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package IO;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import Configuration.*;
/**
 *
 * @author Randy
 * Basic IO operations needed for the application
 */
public class ResourceLocationHelper {
    public ResourceLocationHelper() {
        ///put some stuff later
    }
    /////Checks if a specified folder exists
    public boolean folderExist(String folderName) {
        String basePath = new File("").getAbsolutePath()+"//"+Configuration.MasterFolder;
        Path path; 
        if (folderName == "")
            path = Paths.get(basePath);
        else    
            path = Paths.get(basePath+"//"+folderName);
        return Files.exists(path);
    }
    ///looks for a folder with a specified prefix
    public int folderHasPrefixMatchContent(String folderName, String prefixString) {
        String basePath = new File("").getAbsolutePath()+"//"+Configuration.MasterFolder;
        File folder;
        int count = 0;
        try {
            folder = new File(basePath+"//"+folderName);
        } catch(Exception e) {
            return 0;
        }
        for (File file : folder.listFiles()) {
            if (file.getName().startsWith(prefixString))
                count++;
        }
        return count;
    }
    ///looks checks if a folder has a file with a specified extension
    public int folderHasExtensionMatchFiles(String folderName, String extension) {
        String basePath = new File("").getAbsolutePath()+"//"+Configuration.MasterFolder;
        File folder;
        int count = 0;
        try {
            folder = new File(basePath+"//"+folderName);
        } catch(Exception e) {
            return 0;
        }
        for (File file : folder.listFiles()) {
            String name = file.getName();
            if (name.endsWith("."+extension)) 
                count++;
        }
        return count;
    }
    
    public File[] getFolders(String folderName, String masterFolder) {
         String  basePath = new File("").getAbsolutePath()+"//"+Configuration.MasterFolder+"//"+folderName;
         File folder = new File(basePath);
         return folder.listFiles();
    } 
    
    public int CheckContentFolderChilds(String folderName, String extension) {
        String  basePath = new File("").getAbsolutePath()+"//"+Configuration.MasterFolder+"//"+folderName;
        File folder = new File(basePath);
        int count = 0;
        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                if (this.folderHasExtensionMatchFiles(folderName+"//"+file.getName(), extension)>0)
                    count++;
            }
        }
        return count;
    }
    
    
    
    public int CheckContentFolderChildsPersonajes(String folderName, String extension) {
        String  basePath = new File("").getAbsolutePath()+"//"+Configuration.MasterFolder+"//"+folderName;
        File folder = new File(basePath);
        int count = 0;
        for (File file : folder.listFiles()) {
            if (file.isDirectory() && file.getName().startsWith("P-")) { 
                 if (this.folderHasExtensionMatchFiles(folderName+"//"+file.getName(), extension)>0)
                    count++;
            }
        }
        return count;
    }
}
